<!DOCTYPE html>
<html>
<head>
	<title>AJAX</title>
	<script type="text/javascript" src='jquery.js'></script>
</head>
<body>
	<button onclick="myajax()">点击</button>
</body>
<script type="text/javascript">
	function myajax(){
		$.ajax({
			type: "post",
			url: "./mytext.php",
			data: {
				"para":1
			},
			cache: false,
			async : true,
			dataType: "json",
			success: function (data ,textStatus, jqXHR)
			{
				if("true"==data.flag){
				   alert("合法！");
					return true;
				}else{
					alert("不合法！错误信息如下："+data.errorMsg);
					return false;
				}
			},
			error:function (XMLHttpRequest, textStatus, errorThrown) {
				console.log(XMLHttpRequest);      
				console.log(textStatus);      
				console.log(errorThrown);      
				alert("请求失败！");
			}
		});
	}
	// 封装ajax函数
	function deajax(theurl, postdata){
		$.ajax({
			type: "post",
			url: theurl,
			data: { postdata },
			async : true,
			dataType: "json",
			success: function (data)
			{
				console.log(data);
			},
			error:function (XMLHttpRequest, textStatus, errorThrown) {
				console.log(XMLHttpRequest);      
				console.log(textStatus);      
				console.log(errorThrown);      
				alert("请求失败！");
			}
		});
	}
	// 封装post函数
	function depost(theurl, postdata){
		$.post(theurl, { postdata },
			function(data){
			console.log(data);
		}, "json");
	}


























	
	// 事件处理对象
  
var EventUtil = {
   
 // 添加事件监听 
 add: function(element, type, callback){
	
  if(element.addEventListener){
   element.addEventListener(type, callback, false);
  } else if(element.attachEvent){
   element.attachEvent('on' + type, callback);
  } else {
   element['on' + type] = callback;
  }
 },
  
 // 移除事件监听
 remove: function(element, type, callback){
	
  if(element.removeEventListener){
   element.removeEventListener(type, callback, false);
  } else if(element.detachEvent){
   element.detachEvent('on' + type, callback);
  } else {
   element['on' + type] = null;
  }
  
 },
  
 // 跨浏览器获取 event 对象
 getEvent: function(event){
   
  return event ? event : window.event;
 },
  
 // 跨浏览器获取 target 属性
 getTarget: function(event){
	
  return event.target || event.srcElement;
 },
  
 // 阻止事件的默认行为
 preventDefault: function(event){
   
  if(event.preventDefault){
   event.preventDefault();
  } else {
   event.returnValue = false;
  }
 },
  
 // 阻止事件流或使用 cancelBubble
 stopPropagation: function(){
	
  if(event.stopPropagation){
   event.stopPropagation();
  } else {
   event.cancelBubble = true;
  }
 }
  
};
  
// 使用例子
var at = document.getElementbyId('atemp');
EventUtil.add(at, 'click', function(){
  
 console.log('被点击了');
  
 event = EventUtil.getEvent(event); // 跨浏览器获取 event 对象
 EventUtil.preventDefault(event); // 阻止默认事件
});
</script>
</html>